package main

import (
	"fmt"
	"net/http"
	"os"
	"rabbit/build"
	"runtime"

	"github.com/erikdubbelboer/gspt"
	"github.com/gin-gonic/gin"
	"github.com/urfave/cli"
)

var runCmd = cli.Command{
	Name:            "run",
	ShortName:       "",
	Usage:           "Start a rabbit pusher server",
	SkipFlagParsing: true, SkipArgReorder: true,
	Action: func(*cli.Context) error {
		fmt.Println("rabbit event push server ready!")

		router := gin.Default()
		router.GET("/pusher-ready", func(c *gin.Context) {
			c.JSON(http.StatusOK, nil)
		})

		router.Run(":8083")

		return nil
	},
}

func main() {
	// hide process arguments from ps output, since they may contain
	// database credentials or other secrets.
	gspt.SetProcTitle("rabbit " + os.Args[0])

	app := cli.NewApp()
	app.Name = build.AppName
	app.Usage = "rabbit im, the lightweight real-time communication tools. pusher server"
	app.Version = fmt.Sprintf("%s (%s)", build.Version, build.GitCommit)
	app.Author = "tang shun cai"
	app.Commands = []cli.Command{
		runCmd,
	}

	cli.VersionPrinter = func(c *cli.Context) {
		fmt.Printf("%s version %s\n", app.Name, app.Version)
		fmt.Printf("go version %s\n", runtime.Version())
	}
	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:  "data-dir,d",
			Usage: "(data) Folder to hold state default /var/lib/rabbit/data/" + build.Program + " or ${HOME}/.rabbit/" + build.Program + " if not root",
		},
	}

	if err := app.Run(os.Args); err != nil {
		fmt.Println(err)
	}
}
